package com.lingyuan.product.config;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.lingyuan.product.domain.UserExcel;
import com.lingyuan.product.mapper.UserMapper;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ExcelUserExcelDTOListener
 * @Description 描述
 * @Author guoxin
 * @Date 2024/9/24 21:29
 */
@Log4j2
@NoArgsConstructor
public class ExcelUserExcelDTOListener extends AnalysisEventListener<UserExcel> {
    //数据存储列表
    private List<UserExcel> list = new ArrayList<>();
    //每记录10条数据后就进行存储
    private static  final int BATCH_COUNT = 10;
    long start= 0;
    long end = 0;
    private UserMapper userMapper;
    public ExcelUserExcelDTOListener(UserMapper userMapper){
        this.userMapper = userMapper;
    }
    @Override
    public void invoke(UserExcel userExcel, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", userExcel);
        long l = System.currentTimeMillis();
        log.info("入库单导入开始时间:{}"+start);
        //将数据填入到数据列表中
        list.add(userExcel);
        if (list.size()>=BATCH_COUNT){
            userMapper.insertSelective(list);
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        userMapper.insertSelective(list);
        end = System.currentTimeMillis();
        long result = end - start;
        log.info("导入所用时间:{}"+result);
    }
}
